package service;

import entity.Student;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.time.LocalDate;

//学生管理类
public class StudentManager {
    //用Map存储学生对象
    private final Map<String,Student> studentMap=new HashMap<>();

    public Map<String, Student> getStudentMap() {
        return studentMap;
    }

    @Override
    public String toString() {
        return "service.StudentManager{" +
                "studentMap=" + studentMap +
                '}';
    }

    //显示全部学生
    public void showStudents(){
        if(studentMap.isEmpty()){
            System.out.println("暂无学生");
            return;
        }
        for(Student student:studentMap.values()){
            System.out.println(student);
        }
    }

    //根据学号添加学生
    public void addStudent(Student student) {
        if (studentMap.containsKey(student.getStudentID())) {
            System.out.println("学号已经存在，添加失败");
            return;
        }
        studentMap.put(student.getStudentID(), student);
        System.out.println("添加成功");
    }

    //根据学号删除学生
    public void deleteStudent(String studentID) {
        if (studentMap.containsKey(studentID)){
            studentMap.remove(studentID);
            System.out.println("删除成功");
        } else {
            System.out.println("学号不存在，删除失败");
        }
    }

    //修改信息
    public void updateStudent(Student updateStudent) {
        if (studentMap.containsKey(updateStudent.getStudentID())) {
            studentMap.put(updateStudent.getStudentID(), updateStudent);
            System.out.println("更新成功");
        } else {
            System.out.println("学号不存在，更新失败");
        }
    }

    //查询信息
    public Student getStudentById(String studentID) {
        if (studentMap.containsKey(studentID)) {
            return studentMap.get(studentID);
        } else {
            System.out.println("未找到该学号的学生");
            return null;
        }
    }
    
    /**
     * 根据姓名搜索学生
     */
    public List<Student> searchStudentsByName(String name) {
        List<Student> result = new ArrayList<>();
        for (Student student : studentMap.values()) {
            if (student.getName().contains(name)) {
                result.add(student);
            }
        }
        return result;
    }
    
    /**
     * 根据专业搜索学生
     */
    public List<Student> searchStudentsByMajor(String major) {
        List<Student> result = new ArrayList<>();
        for (Student student : studentMap.values()) {
            if (student.getMajor().contains(major)) {
                result.add(student);
            }
        }
        return result;
    }
    
    /**
     * 根据入学年份搜索学生
     */
    public List<Student> searchStudentsByEnrollYear(int year) {
        List<Student> result = new ArrayList<>();
        for (Student student : studentMap.values()) {
            if (student.getEnrollDate().getYear() == year) {
                result.add(student);
            }
        }
        return result;
    }
    
    /**
     * 获取指定年级的学生
     */
    public List<Student> getStudentsByGrade(String grade) {
        List<Student> result = new ArrayList<>();
        for (Student student : studentMap.values()) {
            if (student.getGrade().equals(grade)) {
                result.add(student);
            }
        }
        return result;
    }
    
    /**
     * 获取学生总数
     */
    public int getTotalStudents() {
        return studentMap.size();
    }
    
    /**
     * 获取各专业学生分布
     */
    public Map<String, Integer> getMajorDistribution() {
        Map<String, Integer> distribution = new HashMap<>();
        for (Student student : studentMap.values()) {
            String major = student.getMajor();
            distribution.put(major, distribution.getOrDefault(major, 0) + 1);
        }
        return distribution;
    }
}